package com.cmcc.waf.report.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.cmcc.waf.report.feign.model.ResponseVO;
import com.cmcc.waf.report.feign.model.SrcIpTopVO;
import com.cmcc.waf.report.feign.model.ThreatTypeVO;
import com.cmcc.waf.report.feign.model.WebSafeRequest;
import com.cmcc.waf.report.service.AttackSrcService;
import com.cmcc.waf.report.utils.EcloudResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
public class AttackSrcController {
    @Autowired
    private AttackSrcService attackSrcService;

    @PostMapping("/attack-src/top")
    public EcloudResult<List<SrcIpTopVO>> srcIpTop5(@RequestBody @Validated WebSafeRequest request) {
        request.setStartTime(StrUtil.isEmpty(request.getStartTime()) ? DateUtil.beginOfDay(DateUtil.date()).toString() : request.getStartTime());
        request.setEndTime(StrUtil.isEmpty(request.getEndTime()) ? DateUtil.now() : request.getEndTime());
        ResponseVO<List<SrcIpTopVO>> response = attackSrcService.getSrcIpTop5(request);
        if (response.success()) {
            return new EcloudResult<List<SrcIpTopVO>>().ok(response.getBody());
        } else {
            return new EcloudResult<List<SrcIpTopVO>>().error(Integer.parseInt(response.getCode()), response.getMessage());
        }
    }
}
